import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  TaskApi,
  StartProcInstReq,
  FlowProcInstModel,
  TaskKey,
  TaskReq,
  FlowTaskPage,
  FlowClaimTaskRequest,
  FlowTaskModel,
  FlowCompleteTaskRequest,
  FlowTaskDefList,
  FlowGobackTaskRequest,
  FlowAssignTaskRequest,
  FlowPriorityTaskRequest,
  FlowDelegateTaskRequest,
  FlowResolveTaskRequest,
  TaskUserReq,
  TaskUserPage,
  FlowProcInstKey,
} from "./types";

class TaskApiImpl implements TaskApi {
  getKey(model: FlowTaskModel): TaskKey {
    return {
      taskId: model.id,
    } as TaskKey;
  }
  // 获取我的待办事项
  findPage(req: TaskReq): AxiosPromise<FlowTaskPage> {
    const tenant = useUserStore().user.tenant;
    if (req.priority) {
      req.hasPriority = req.priority != undefined;
    }
    clearEmpty(req);
    return request({
      url: "/v1/flow/task/mytodo/page/" + tenant,
      method: "get",
      params: req,
    });
  }
  // 启动流程实例
  startProcInst(data: StartProcInstReq): AxiosPromise<FlowProcInstModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/procinst/" + tenant,
      method: "post",
      data: data,
    });
  }

  // 批量 认领/取消认领/强制取回 任务
  claimTask(data: FlowClaimTaskRequest): AxiosPromise<number> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/claim/" + tenant,
      method: "post",
      data: data,
    });
  }
  // 查询任务详情
  findTaskById(taskId: string): AxiosPromise<FlowTaskModel> {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/flow/task/id/" + tenant + "/" + taskId,
      method: "get",
    });
  }
  // 查询流程实例历史办理记录
  findProcInstDiagram(procInstId: string): AxiosPromise {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/flow/procinst/list/" + tenant + "/" + procInstId,
      method: "get",
      responseType: "blob",
    });
  }
  // 执行任务
  completeTask(data: FlowCompleteTaskRequest): AxiosPromise<boolean> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    const module = data.module ? "/" + data.module : "";
    return request({
      url: "/v1" + module + "/flow/task/complete/" + tenant,
      method: "post",
      data: data,
    });
  }
  // 获取允许退回的当前流程历史历史任务
  findTaskDefAllowGoback(taskId: string): AxiosPromise<FlowTaskDefList> {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/flow/taskdef/goback/list/" + tenant + "/" + taskId,
      method: "get",
    });
  }
  // 任务退回到选择节点
  gobackTask(data: FlowGobackTaskRequest): AxiosPromise<boolean> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/goback/" + tenant,
      method: "post",
      data: data,
    });
  }
  //  分配/移交任务给某人或某些候选用户.
  assignTask(data: FlowAssignTaskRequest): AxiosPromise<boolean> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/assign/" + tenant,
      method: "post",
      data: data,
    });
  }
  // 设置任务优先级.
  priorityTask(data: FlowPriorityTaskRequest): AxiosPromise<boolean> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/priority/" + tenant,
      method: "post",
      data: data,
    });
  }
  // 委托用户办理任务
  delegateTask(data: FlowDelegateTaskRequest): AxiosPromise<boolean> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/delegate/" + tenant,
      method: "post",
      data: data,
    });
  }
  // 完成委托任务
  resolveTask(data: FlowResolveTaskRequest): AxiosPromise<boolean> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/flow/task/resolve/" + tenant,
      method: "post",
      data: data,
    });
  }

  // 查询任务用户 (监控监控/ 待办任务移交 公共组件查询候选人接口)
  findTaskUser(req: TaskUserReq): AxiosPromise<TaskUserPage> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(req);
    return request({
      url:
        "/v1/flow/task/identity_link/" +
        tenant +
        "/" +
        req.taskId +
        "/" +
        req.type,
      method: "get",
    });
  }

  // 获取流程实例
  findProcinstId(req: FlowProcInstKey): AxiosPromise<FlowCompleteTaskRequest> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(req);
    return request({
      url: "/v1/flow/procinst/vars/" + tenant + "/" + req.procInstId,
      method: "get",
    });
  }
}
const taskApi = new TaskApiImpl();

export { taskApi };
